package jsu.ljy.lostandfind.controller;

import jsu.ljy.lostandfind.common.Result;
import jsu.ljy.lostandfind.model.Comment;
import jsu.ljy.lostandfind.model.User;
import jsu.ljy.lostandfind.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 评论管理控制器
 * 
 * <p>处理评论相关的HTTP请求，包括评论的发表、修改、删除等操作。</p>
 * 
 * @author 罗嘉毅
 * @version 1.0
 * @since 2024-03-21
 */
@RestController
@RequestMapping("/api/comments")
public class CommentController {
    
    @Autowired
    private CommentService commentService;

    /**
     * 获取评论列表
     * 
     * @param itemId 物品ID(可选)
     * @param userId 用户ID(可选)
     * @return 评论列表
     */
    @GetMapping
    public Result<List<Comment>> getComments(
            @RequestParam(required = false) Long itemId,
            @RequestParam(required = false) Long userId) {
        
        if (itemId != null) {
            return Result.success(commentService.getCommentsByItemId(itemId));
        } else if (userId != null) {
            return Result.success(commentService.getCommentsByUserId(userId));
        } else {
            return Result.error(400, "必须指定itemId或userId参数");
        }
    }

    @GetMapping("/{id}")
    public Result<Comment> getComment(@PathVariable Long id) {
        Comment comment = commentService.getCommentById(id);
        if (comment != null) {
            return Result.success(comment);
        }
        return Result.error(404, "评论不存在");
    }

    @PostMapping
    public Result<Comment> createComment(@RequestBody Comment comment, HttpSession session) {
        User currentUser = (User) session.getAttribute("currentUser");
        if (currentUser == null) {
            return Result.error(401, "请先登录");
        }

        comment.setUserId(currentUser.getId());
        try {
            commentService.publishComment(comment);
            return Result.success(comment);
        } catch (IllegalArgumentException e) {
            return Result.error(400, e.getMessage());
        }
    }

    @PutMapping("/{id}")
    public Result<Comment> updateComment(
            @PathVariable Long id,
            @RequestBody Comment comment,
            HttpSession session) {
        
        User currentUser = (User) session.getAttribute("currentUser");
        if (currentUser == null) {
            return Result.error(401, "请先登录");
        }

        Comment existingComment = commentService.getCommentById(id);
        if (existingComment == null) {
            return Result.error(404, "评论不存在");
        }

        if (!currentUser.getId().equals(existingComment.getUserId())) {
            return Result.error(403, "只能修改自己的评论");
        }

        comment.setId(id);
        try {
            commentService.updateComment(comment);
            return Result.success(comment);
        } catch (IllegalArgumentException e) {
            return Result.error(400, e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public Result<Void> deleteComment(@PathVariable Long id, HttpSession session) {
        User currentUser = (User) session.getAttribute("currentUser");
        if (currentUser == null) {
            return Result.error(401, "请先登录");
        }

        Comment comment = commentService.getCommentById(id);
        if (comment == null) {
            return Result.error(404, "评论不存在");
        }

        if (!currentUser.getId().equals(comment.getUserId())) {
            return Result.error(403, "只能删除自己的评论");
        }

        commentService.deleteComment(id);
        return Result.success(null);
    }
} 